package dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.zx.theline.R;

import org.apache.http.Header;

import GsonBean.ShyjhxqBean;
import GsonBean.SuccessBean;
import GsonBean.YjhshthBean;
import GsonBean.ZsyjhxxShBean;
import GsonBean.post.SgbThPost;
import Utils.DateUtil;
import Utils.DeviceUuidFactory;
import Utils.MyToast;
import app.MyApplication;
import base.BaseDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Ren on 2017/8/28.
 * 月计划 退回
 */

public class ThDialog extends BaseDialog implements View.OnClickListener {

    private ShyjhxqBean mZsyjhxxShBean;
    private RadioButton sgdw, xmzgbm;
    private EditText editText;
    private TextView confirm, cancel;
    private View.OnClickListener onClickListener;
    //项目主管部门退回 PostBean
    private YjhshthBean mYjhshthBean;
    //施工办退回 PostBean
    private SgbThPost mSgbThPost;
    private int flag=-1;
    public static int SGBSPTH = 0;
    public static int SHTH = 1;

    public ThDialog(Context context, int flag, ShyjhxqBean mZsyjhxxShBean, View.OnClickListener onClickListener) {
        super(context, R.style.Theme_dialog);
        this.onClickListener = onClickListener;
        this.mZsyjhxxShBean = mZsyjhxxShBean;
        this.flag=flag;

    }

    public ThDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ThDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_th);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {

        setCanceledOnTouchOutside(false);


        sgdw = (RadioButton) findViewById(R.id.sgdw);
        xmzgbm = (RadioButton) findViewById(R.id.xmzgbm);
        editText = (EditText) findViewById(R.id.et_thyj);
        confirm = (TextView) findViewById(R.id.confirm);
        cancel = (TextView) findViewById(R.id.cancel);

        if (flag == SHTH) {
            hideXmzgbm();
        }
    }

    @Override
    protected void InitData() {

    }

    @Override
    protected void InitListen() {
        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                dismiss();
                break;
            case R.id.confirm:
                if (!sgdw.isChecked() && !xmzgbm.isChecked()) {
                    MyToast.showToast("请选择退回的部门");
                    return;
                }
                if (editText.getText().length() == 0) {
                    MyToast.showToast("请填写退回意见");
                    return;
                }

                if (editText.getText().length() > 0 && editText.getText().toString().trim().length()==0) {
                    MyToast.showToast("请不要输入空格");
                    return;
                }
                //审核退回 施工单位
                if (flag==SHTH) {
                    mYjhshthBean = new YjhshthBean();
                    mYjhshthBean.setJdjhbBh(mZsyjhxxShBean.getInfo().getBh());
                    mYjhshthBean.setSftg("TH");
                    mYjhshthBean.setShyj(editText.getText().toString());
                    mYjhshthBean.setShrq(DateUtil.getCurDate());
                    mYjhshthBean.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    mYjhshthBean.setDwlb("spcs");
                    mYjhshthBean.setShzt(mZsyjhxxShBean.getInfo().getShzt());
                    mYjhshthBean.setShrname(MyApplication.getmLoginUser().getTrueName());
                    mYjhshthBean.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    MyApplication.ShowWaitDialog(getContext());
                    NetWorkApi.shyjhth(mYjhshthBean, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    try {
                                        SuccessBean Bean = response.body();
                                        if (Bean.getMsg().equals("success")) {
                                            onClickListener.onClick(confirm);
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );
                }
                //退回项目主管部门
                if(flag==SGBSPTH){
                    mSgbThPost=new SgbThPost();
                    mSgbThPost.setShzt(mZsyjhxxShBean.getInfo().getShzt());
                    if(sgdw.isChecked()){
                        mSgbThPost.setSgbReturnType("sgdw");
                    }
                    if(xmzgbm.isChecked()){
                        mSgbThPost.setSgbReturnType("spcs");
                    }
                    mSgbThPost.setJdjhbBh(mZsyjhxxShBean.getInfo().getBh());
                    mSgbThPost.setOpeartionType("3");
                    mSgbThPost.setOpeartionName("退回");
                    mSgbThPost.setOpeartionWorkername(MyApplication.getmLoginUser().getTrueName());
                    mSgbThPost.setOpeartionIp(DeviceUuidFactory.getDeviceId(getContext()));
                    mSgbThPost.setDeleteFlag("N");
                    mSgbThPost.setCjrId(MyApplication.getmLoginUser().getUserDisplayName());
                    mSgbThPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());
                    SgbThPost.ShdwInfo mShdwInfo=new SgbThPost.ShdwInfo();
                    mShdwInfo.setJdjhbBh(mZsyjhxxShBean.getInfo().getBh());
                    mShdwInfo.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    mShdwInfo.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    mShdwInfo.setDwlb("sgb");
                    mShdwInfo.setShsx("130");
                    mShdwInfo.setSpzd("0");
                    mShdwInfo.setShlb("5");
                    mShdwInfo.setDylb("Y");
                    mShdwInfo.setDeleteFlag("N");
                    mShdwInfo.setCjrId(MyApplication.getmLoginUser().getUserDisplayName());
                    mShdwInfo.setSftg("TH");
                    mShdwInfo.setShyj(editText.getText().toString());
                    mShdwInfo.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    mShdwInfo.setShrname(MyApplication.getmLoginUser().getTrueName());
                    mShdwInfo.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    mSgbThPost.setShdwInfo(mShdwInfo);
                    MyApplication.ShowWaitDialog(getContext());
                    NetWorkApi.sgbth(mSgbThPost, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    try {
                                        SuccessBean Bean = response.body();

                                        if (Bean.getMsg().equals("success")) {
                                            onClickListener.onClick(confirm);
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }

                    );

                }
        }
    }

    //隐藏项目主管部门选项
    private void hideXmzgbm() {
        xmzgbm.setVisibility(View.GONE);
    }
}
